package com.jmb.apiplatform.service.impl;


import com.jmb.apiplatform.exception.ValidException;
import com.jmb.apiplatform.mapper.PermissionMapper;
import com.jmb.apiplatform.pojo.PermissionDao;
import com.jmb.apiplatform.pojo.PermissionTO;
import com.jmb.apiplatform.service.PermissionService;
import com.jmb.apiplatform.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void savePermission(PermissionDao permissionDao) throws ValidException {
        ValidUtil.notNUll(permissionDao.getPermissionCode(),"权限代码不能为空");
        ValidUtil.length(permissionDao.getPermissionCode(),1,20,"权限代码长度必须小于等于20");
        ValidUtil.notNUll(permissionDao.getPermissionName(),"权限名称不能为空");
        ValidUtil.length(permissionDao.getPermissionName(),1,20,"权限名称长度必须小于等于20");
        String permissionCode = permissionDao.getPermissionCode();
        if(finPermissionCode(permissionCode)!=null){
            throw new ValidException("权限代码已存在");
        }
        permissionDao.setParentId(permissionDao.getParentId()==null?0: permissionDao.getParentId());
        permissionMapper.insertPermission(permissionDao);
    }

    @Override
    public PermissionDao finPermissionCode(String permissionName) {

        return permissionMapper.finPermissionCode(permissionName);
    }

    @Override
    public List<PermissionTO> findPermission(Integer parentId) {
       return permissionMapper.findPermission(parentId);
    }

    @Override
    public void modifyPermission(PermissionDao permissionDao) throws ValidException {
        ValidUtil.notNUll(permissionDao.getPermissionCode(),"权限代码不能为空");
        ValidUtil.length(permissionDao.getPermissionCode(),1,20,"权限代码长度必须小于等于20");
        ValidUtil.notNUll(permissionDao.getPermissionName(),"权限名称不能为空");
        ValidUtil.length(permissionDao.getPermissionName(),1,20,"权限名称长度必须小于等于20");
        String permissionCode = permissionDao.getPermissionCode();
        Integer id = permissionDao.getId();
        List<PermissionDao> permissionDaos = checkPermission(id, permissionCode);
        if (permissionDaos.size()>0){
            throw new ValidException("权限代码已存在");
        }
        permissionMapper.modifyPermission(permissionDao);
    }

    @Override
    public List<PermissionDao> checkPermission(Integer id, String permissionCode) throws ValidException {
        if (id==null && permissionCode==null){
            log.info("传入的id是{},code是{}",id, null);
            throw new ValidException("ID和permissionCode不能为空");
        }
        return permissionMapper.checkPermission(id, permissionCode);
    }

    @Override
    public void removeById(Integer id) throws ValidException {
        if (id==null){
            throw new ValidException("传入的id不能为空");
        }
        List<PermissionTO> permissionList = findPermissionList(id);
        if (permissionList.size()>0){
            throw new ValidException("该权限下有子权限，请先删除子权限");
        }
        // 删除这边权限的时候，也需要把配置的对应角色权限进行删除，也就是role_permission表进行删除
        permissionMapper.deletePermissionRoleByPermissionId(id);
        permissionMapper.removeById(id);
    }

    @Override
    public List<PermissionTO> findPermissionList(Integer permissionId) {
        return permissionMapper.findPermissionList(permissionId);
    }


}
